/**
 * handle edit group page logic.
 */
function pageinit() {
	$('#groupForm').validate({ // initialize the plugin
		rules : {
			"textinput-name" : {
				required : true,
				minlength : 2,
				maxlength : 20,
				remote : {
					url : "/checkGroupName",
					dataType : "json",
					data : {
						"gwid" : function() {
							return $("#editGroupDlg #textinput-gwid").val()
						},
						"groupId" : function() {
							return $("#editGroupDlg #textinput-id").val()
						}
					},
					dataFilter : function(result) {
						if (result != "true") {
							return "\"" + result + "\"";
						} else {
							return '"true"';
						}
					}
				}
			},
			"textinput-type" : {
				required : true,
				minlength : 2,
				maxlength : 6,
				remote : {
					url : "/checkGroupType",
					dataFilter : function(result) {
						if (result != "true") {
							return "\"" + result + "\"";
						} else {
							return '"true"';
						}
					}
				}
			},
			"textinput-gwid" : {
				required : true,
				minlength : 2,
				maxlength : 20,
				remote : {
					url : "/checkGroupGwId",
					dataFilter : function(result) {
						if (result != "true") {
							return "\"" + result + "\"";
						} else {
							return '"true"';
						}
					}
				}
			},
			"textinput-description" : {
				required : true,
				minlength : 2,
				maxlength : 20,
			}
		},

		invalidHandler : function() {
			//console.log('invalid form');
		},

		submitHandler : function(form) {
			//console.log("submitHandler");
			$(form).ajaxSubmit({
				type : 'post',
				url : "/_saveGroup",
				success : function(data) {
					if (data == "ok") {
						window.history.back();
					} else {
						var err = 'save group config err:' + data;
						//console.log(err);
						showMsgPopup(err, "#editGroupDlg");
					}
				},
				error : function(XmlHttpRequest, textStatus, errorThrown) {
					//console.log('ajaxSubmit failed:' + errorThrown);
				}
			});
		}
	});
}

$(document).on('pageinit', function() {
	pageinit();
});
